package com.slz3.business.frame.api.alipay;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.slz3.business.frame.api.tool.AppEnv;
import com.slz3.business.frame.api.wx.DirectPayApi;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 验证是否是支付宝官网的信息
 * @author zhaozhongxin
 *
 */
public class AlipayUtilApi {
	/*日志记录*/
	protected final static Log logger = LogFactory.getLog(AlipayUtilApi.class);
	/*支付宝合作者身份ID*/
	private static final String PARTNER= AppEnv.getInstance().getStringProp("alipay.partner", "2018071460726101");
	/*接口名称*/
	private static final String SERVICE=AppEnv.getInstance().getStringProp("alipay.service", "notify_verify");;
	/*接口名称*/
	private static final String QUERY_URL=AppEnv.getInstance().getStringProp("alipay.queryurl", "http://mapi.alipay.com/gateway.do");;
	/*回调路径*/
	public static final String RETURN_URL=AppEnv.getInstance().getStringProp("alipay.returnurl", "");
	/*回调路径*/
	//public static final String NOTIFY_URL=AppEnv.getInstance().getStringProp("alipay.notifyurl", "http://f12d026b.ngrok.io/appPay!appUserAlipayPay.action");
	
	public static final String NOTIFY_URL="http://60.205.208.183:8081/appPay!appUserAlipayPay.action";

	
	/*私钥*/
	public static final String PRIVATE_KEY=AppEnv.getInstance().getStringProp("alipay.privateKey", "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDBD+DHHrw3GyqCsJ7h5G7obbNXMNjMsW0tVPuSdXTQucN4YCSL3E3Fh+UV6hrcSKoGfySVWhY9yyHjY5P0mooaDIbVwYEqj/EMfIxBP+IE14kAvD2sbT0A6PU+P9RyQZyDK3U/2WEppU52r23IxgsKTwaaGrrBUOOEZ3PTS74FBqb9CDkY3sHnLbGYFqpwVuqmcRoduhBbho6RcjQJnlvffQ5jpiP6Le1QN83mdxXInyerJiFKDCWd4E/T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AU6MuSObHprISB7eHo5+iXt2sL52/I2NsyFdFXooqxjjXNc5TG6UWErXSs7OhJdxZQgIewKBgQDR+ccyjzua0sYQ7ftwocNedAqijiSPBBt0yxBzn9GhHyeORoMNrZNdqku27voQkAEojwM4lx2ebcuzaKo79ujX4pwGQboX2aNcZMlz1fJ/gTUDEmuN1qFqFsusjnOIVASWYH1gKJhX1K1qhKNiArHcPQP7wYVDmIhRoDwFsYZwpw==");
	/*公钥*/
	public static final String PUBLIC_KEY=AppEnv.getInstance().getStringProp("alipay.publicKey", "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnpRS6k148Tqop4M9TQTtYzjaNJMVwAZuapcj6kQzQVUd3fA5ditHzNH8qAD/MdgdjI1zbw29fj1Q5tT7pdginCC8WCy+52pZQo2D3AwjM6abkC/XcGRlPvySpaog8INMWTX8fhT572R21H9gF9aHpAtYZGSz1rvCp/dTkxcaf/lnKnl6ffcAlBGDl1OiY1fYW4UIOA/ocMJj24+0MTsvDMnf93nl8rRNrq1cVE+z7NoJi4qCL99hH17pczaHG20kj95EUJEMuY3KchOtSCY87k88cCivMENVqXJEGTT1ttZtvP/eNZqWrH5yr+wB9vbMEtA0K1tQUdB9UK7tlj2wMQIDAQAB");

	/**
	 * 支付宝安全验证
	 * @param notify_id 通知校验ID
	 * @return
	 */
	public static String voilidata(String notify_id){
		
		HttpClient client= new HttpClient();
		PostMethod postMethod = new PostMethod(QUERY_URL);//POST请求 支付宝查询路径
		postMethod.addParameter("service", SERVICE);
		postMethod.addParameter("partner", PARTNER);
		postMethod.addParameter("notify_id", notify_id);
		String response="false";
		try {
			client.executeMethod(postMethod);
			if (postMethod.getStatusCode() == HttpStatus.SC_OK) {
				response = postMethod.getResponseBodyAsString();
			}
			//如果返回301,302需要继续请求
			else if (postMethod.getStatusCode()==HttpStatus.SC_MOVED_PERMANENTLY
					||postMethod.getStatusCode()==HttpStatus.SC_MOVED_TEMPORARILY) {
				Header locationHeader = postMethod.getResponseHeader("location");  
				String location = null;  
		         if (locationHeader != null) {  
		            location = locationHeader.getValue();  
		            PostMethod redirect = new PostMethod(location);  
		            client.executeMethod(redirect); 
		            if (postMethod.getStatusCode() == HttpStatus.SC_OK) {
						response = postMethod.getResponseBodyAsString();
					}
		         }
			}
		} catch (IOException e) {
			logger.error("执行HTTP Post请求https://mapi.alipay.com/gateway.do时，发生异常！", e);
		} finally {
			postMethod.releaseConnection();
		}
		return response;
	}
	/**
     * 构建支付订单的参数map
     * @return
     */
    public static Map<String, String> getPayDirectParamMap(String outTradeNo,String totalprice,String bodys){
    	DirectPayApi pay = new DirectPayApi();
    	
    	double total_fee = 0d;
    	StringBuilder body = new StringBuilder();

		Double price =Double.valueOf(totalprice);
    	
    	if(price > 0){
    		total_fee += price;
			body.append(bodys+"|");
    	}
    	
    	pay.setOut_trade_no(outTradeNo);
    	pay.setTotal_fee(String.valueOf(total_fee));
    	pay.setBody(body.toString());
    	pay.setReturn_url(RETURN_URL);
    	pay.setNotify_url(NOTIFY_URL);
    	
		Map<String, String> pmaps = pay.getSignMap();
		return pmaps;
    }
    public static void main(String[] args) {
    	voilidata("2b40e6f60b512f2177d73f8b945a70dm3i");
	}
    
    public static Map<String,Object> canshu()
    {
    	Map<String,Object> map=new HashMap<String, Object>();
    	map.put("PARTNER", PARTNER);
    	map.put("NOTIFY_URL", NOTIFY_URL);
    	map.put("PRIVATE_KEY", PRIVATE_KEY);
    	map.put("PUBLIC_KEY", PUBLIC_KEY);

		return map;
    	
    	
    }
}
